﻿(function () {
    var myApp = angular.module('MyControllers');

    // 考点信息需要的控制器及方法列表
    myApp.controller('siteController', ['$scope', '$http', '$modal', 'examSiteService', 'utils', function ($scope, $http, $modal, service, utils) {
        /*$scope.show = function () {
            service.getInfo().success(function (data) {
                $scope.data = data.siteInfo;
                $scope.appList = data.appList;

                angular.forEach($scope.appList, function (item) {
                    if ($scope.data.AppID == item.AppID) {
                        // 设置显示当前主办方
                        $scope.curApp = item;
                    }
                });
            });
        };
        $scope.show();*/

        $scope.changeCurApp = function () {
            var app = $scope.curApp;

            service.changeCurApp(app.AppID).success(function () {
                $scope.data.AppUrl = app.AppUrl;
                $scope.data.AppFTPUrl = app.AppFTPUrl;

                utils.notify('成功切换当前主办方！', 'success');
            });
        };
        $scope.changePwd = function () {
            $modal.open({
                templateUrl: '/App/views/ExamSite/ChangePwd.html',
                controller: 'changePwd',
                resolve: {
                    params: function () {
                        return 0;
                    }
                }
            });
        };
        $scope.editApp = function () {
            $modal.open({
                templateUrl: '/App/views/ExamSite/ChangeUrl.html',
                controller: 'changeUrl',
                resolve: {
                    params: function () {
                        return $scope.data;
                    }
                }
            });
        };
        $scope.$root.$on('change', function (event, data) {
            $scope.data.SitePwd = data;
        });
        $scope.$root.$on('change2', function (event, data) {
            $scope.data.appUrl = data.appUrl;
            $scope.data.appFTPUrl = data.ftpUrl;
        });
    }]);

    // 修改密码需要的控制器及方法列表
    myApp.controller('changePwd', ['$scope', '$routeParams', '$modalInstance', 'examSiteService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var methods = {
            save: function () {
                if ($('form#changePwdForm').valid()) {
                    $scope.loadingState = true;
                    service.changePwd($scope.oldPwd, $scope.confirmPwd).success(function (data) {
                        if (data.resultType == 'Success') {
                            $scope.close();

                            $scope.$emit('change', $scope.confirmPwd);
                            utils.notify('密码修改成功！', 'success');
                        } else {
                            utils.notify(data.message, 'warning');
                        }
                        $scope.loadingState = false;
                    });
                }
            },
            close: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
    }]);

    // 修改路径需要的控制器及方法列表
    myApp.controller('changeUrl', ['$scope', '$routeParams', '$modalInstance', 'examSiteService', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, utils, params, $filter) {
        var appId = params.appID;
        $scope.appUrl = params.appUrl;
        $scope.ftpUrl = params.appFTPUrl;

        var methods = {
            save: function () {
                if ($('form#changeUrlForm').valid()) {
                    $scope.loadingState = true;

                    var appUrl = $scope.appUrl;
                    var ftpUrl = $scope.ftpUrl;

                    service.changeUrl(appId, appUrl, ftpUrl).success(function(data) {
                        $scope.loadingState = false;
                        if (data.resultType == 'Success') {
                            $scope.close();

                            $scope.$emit('change2', { appUrl: appUrl, ftpUrl: ftpUrl });

                            utils.notify('修改成功！', 'success');
                        } else {
                            utils.notify(data.Message, 'warning');
                        }
                    });
                }
            },
            close: function () {
                $modalInstance.close(true);
            }
        };
        angular.extend($scope, methods);
    }]);
})();